<?php
namespace Home\Controller;
use Think\Controller;

use Think\Vendor;
Vendor('Xinge.XingeApp');

header('Content-type: text/json; charset=utf-8');

class  NewsController extends Controller {

	public function index(){
		echo "news";
	}


	public function edit(){
		$news['news_id'] = I('news_id');

		$news['news_en'] = I('news_en');
		$news['news_sc'] = I('news_sc');
		$news['news_tc'] = I('news_tc');

		$db_news = M('news');
		$db_news->save($news);

		$this->update();

		echo json_encode(array('success'=>1));
	}

	public function get(){
		$db_news = M('news');
		$news = $db_news->where("news_id =1")->find();
		echo json_encode($news);
	}

	public function add(){
		$db_news = M('news');

		$data['title'] = I('title');

		$db_news->add($data);
		echo json_encode(array('success'=>1));
	}

	public function editTitle(){
		$db_news = M('news');

		$data['title'] = I('title');
		$data['news_id'] = I('news_id');

		$db_news->save($data);
		echo json_encode(array('success'=>1));

	}


	private function update(){
		$db_news = M('news');
		$news = $db_news->where("display=1")->find();

		$filename = "news.xml";
		$xml = "<?xml version='1.0' encoding='utf-8'?>\r\n";
		$xml = $xml."<CyberMedia>\r\n";
		$xml = $xml."	<Menu Name='news_en' Text="."\"".$news['news_en']."\""." />\r\n"; 
		$xml = $xml."	<Menu Name='news_sc' Text="."\"".$news['news_sc']."\""." />\r\n"; 
		$xml = $xml."	<Menu Name='news_tc' Text="."\"".$news['news_tc']."\""." />\r\n"; 
		$xml = $xml."</CyberMedia>";
		$res = file_put_contents($filename, $xml);

	}

	public function display(){
		$db_news = M('news');
		$news_id = I('news_id');

		$news_list = $db_news->select();

		foreach ($news_list as $news) {
			$db_news->where("news_id='%d'", $news['news_id'])->setField('display', 0);
		}
		$db_news->where("news_id='%d'", $news_id)->setField('display', 1);

		$this->update();

		echo json_encode(array('success'=>1));
	}


	public function del(){
		$db_news = M('news');
		$news_id = I('news_id');

		$db_news->where("news_id='%d'", $news_id)->delete();
		echo json_encode(array('success'=>1));

	}
}